package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@Slf4j
@Api(tags = "菜品管理")
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * 新增菜品及口味
     * @param dishDTO
     * @return
     */
    @PostMapping("/admin/dish")
    @ApiOperation("新增菜品及口味")
    public Result save(@RequestBody DishDTO dishDTO){
        log.info("新增菜品及口味参数为：{}",dishDTO);
        dishService.saveWithFlavor(dishDTO);

        //清除缓存数据
        redisTemplate.delete("dish_" + dishDTO.getCategoryId());


        return Result.success();
    }

    /**
     * 菜品分页查询
     * @param dishPageQueryDTO
     * @return
     */
    @GetMapping("/admin/dish/page")
    @ApiOperation("菜品分页查询")
    public Result<PageResult> pageQuery(DishPageQueryDTO dishPageQueryDTO){
        log.info("菜品分页查询参数为：{}",dishPageQueryDTO);
        PageResult pageResult = dishService.pageQuery(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 根据 ids 删除菜品及其关联的口味
     * @param ids
     * @return
     */
    @DeleteMapping("/admin/dish")
    public Result deleteByIds(@RequestParam List<Long> ids){
        log.info("根据ids批量删除菜品及口味：{}",ids);
        dishService.deleteByIds(ids);

        //清除缓存数据
        Set keys = redisTemplate.keys("dish_*");
        redisTemplate.delete(keys);


        return Result.success();
    }

    /**
     * 根据id查询菜品
     * @param id
     * @return
     */
    @ApiOperation("根据id查询菜品")
    @GetMapping("/admin/dish/{id}")
    public Result<DishVO> selectById(@PathVariable Long id){
        log.info("根据id查询菜品：{}",id);
        DishVO dishVO = dishService.selectById(id);
        return Result.success(dishVO);
    }

    @ApiOperation("修改菜品及其口味信息")
    @PutMapping("/admin/dish")
    public Result update(@RequestBody DishDTO dishDTO){
        log.info("修改菜品及其口味信息:{}",dishDTO);

        dishService.updateWithFlavor(dishDTO);

        //清除缓存数据
        Set keys = redisTemplate.keys("dish_*");
        redisTemplate.delete(keys);

        return Result.success();
    }


    /**
     * 根据 分类id 查询菜品
     * @param categoryId
     * @return
     */
    @GetMapping("/admin/dish/list")
    @ApiOperation("根据 分类id 查询菜品")
    public Result<List<Dish>> selectByCategoryId(Long categoryId){
        log.info("根据分类id查询菜品：{}",categoryId);
        List<Dish> dishes = dishService.selectByCategoryId(categoryId);
        return Result.success(dishes);
    }

    @ApiOperation("菜品起售、停售")
    @PostMapping("/admin/dish/status/{status}")
    public Result startOrStop(@PathVariable Integer status, Long id){
        log.info("菜品起售、停售:{},{}",status,id);
        dishService.startOrStop(status,id);

        //清除缓存数据
        Set keys = redisTemplate.keys("dish_*");
        redisTemplate.delete(keys);

        return Result.success();
    }
}
